package com.atguigu.stgy.admin.controller;

import com.atguigu.stgy.admin.service.PaymentTypeService;
import com.atguigu.stgy.model.entity.PaymentType;
import com.atguigu.stgy.model.result.R;
import com.atguigu.stgy.model.result.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*
    跨域配置：
        以后有多个controller 每个单独配置 不方便管理
 */
@RestController
@RequestMapping("/admin/payment")
//@CrossOrigin //解决跨域问题
@Tag(name = "支付方式管理模块")
public class PaymentTypeController {
    //统一响应类： 前端写好了  得按照前端的要求创建
    @Resource
    PaymentTypeService paymentTypeService;
    //查询所有支付方式
    //前端需要的是统一响应类
    /*
        前端js处理数字类型的数据时，长度超过16位的会对后三位四舍五入 精度丢失
        解决： json转换时 将数字转为字符串

     */
    @Operation(summary = "查询所有支付方式")
    @GetMapping("list")
    public R<List<PaymentType>> list(){

        return R.ok(paymentTypeService.list());
    }

    ///admin/payment/deleteById?id=11
    @Operation(summary = "删除指定支付方式")
    @DeleteMapping("deleteById")
    public R deleteById(@Parameter(name = "id" , description = "支付方式id" ,in = ParameterIn.QUERY)
                            @RequestParam(value = "id")Long id){
        boolean b = paymentTypeService.removeById(id);
        return b?R.ok():R.fail(ResultCodeEnum.FAIL);
    }
    @Operation(summary = "新增或更新支付方式")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody PaymentType paymentType){
        //有id代表更新 没有代表新增
        boolean b = paymentTypeService.saveOrUpdate(paymentType);
        return b?R.ok():R.fail(ResultCodeEnum.FAIL);
    }
}
